// loveschool.js
import api from "../../utils/api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current:0,
    courses:[],
    searchKey:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getXiangqinCourses();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.getCourses();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 切换tab页
   */
  changeTab:function(e){
    this.setData({
      current:e.detail.current
    });
  },
  tapChangeTab:function(e){
    this.setData({
      current:e.target.dataset.index
    });
  },
  /**
   * 查询所有课程信息
   */
  getCourses:function(e){
    var that = this;
    console.log("查看课程");
    console.log(e.currentTarget.dataset.type);
    var url = "";
    if (e.currentTarget.dataset.type == "xq"){
        //相亲
        console.log("相亲课程");
      url = api.queryClassXq;
    } else if (e.currentTarget.dataset.type == "la") {
        //恋爱
      console.log("恋爱课程");
      url = api.queryClassHy;
    } else if (e.currentTarget.dataset.type == "hy") {
        //婚姻
      console.log("婚姻课程");
      url = api.queryClassHy;

    } else if (e.currentTarget.dataset.type == "jt") {
        //家庭矛盾
      console.log("家庭关系课程");
      url = api.queryClassJt;
    }
    wx.request({
      url: url,
      data:{},
      method:"POST",
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      success:function(response){
        console.log(response.data.data);
        that.setData({
          courses:response.data.data
        });
      }
    })
  },

  /**
   * 查看相亲课程
   */
  getXiangqinCourses:function(){
    var that = this;
    wx.request({
      url: api.queryClassXq,
      data: {},
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (response) {
        // console.log("获取所有课程信息");
        that.setData({
          courses: response.data.data
        });
      }
    })
  },
  /**
   * 输入搜索关键字
   */
  searchKeyInput:function(e){
    this.setData({
      searchKey:e.detail.value
    });
  },
  /**
   * 执行搜索
   */
  searchCourse:function(){
    var that = this;
    // debugger;
    wx.request({
      url: api.searchCourse,
      data:{
        summary: this.data.searchKey
      },
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      success:function(response){
        that.setData({
          courses: response.data.data
        });
      },
      method:"POST"
    })
  }
})